﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LitJson;

public class Item
{
    public enum ItemType
    {
        NONE = -1,

        // Part
        ENGINE = 0 * 10000 + typeIDInCrement * 0,
        BATTERY = 0 * 10000 + typeIDInCrement * 1,
        DYNAMO = 0 * 10000 + typeIDInCrement * 2,
        SHALL = 0 * 10000 + typeIDInCrement * 3,
        WEAPON_SLOT = 0 * 10000 + typeIDInCrement * 4,
        HOOK = 0 * 10000 + typeIDInCrement * 5,
        SHILD = 0 * 10000 + typeIDInCrement * 6,
        OTHER_PART = 0 * 10000 + typeIDInCrement * 99,

        // Weapon
        ARTILLARY = 100 * 10000 + typeIDInCrement * 0,
        LASER = 100 * 10000 + typeIDInCrement * 1,
        RAILGUN = 100 * 10000 + typeIDInCrement * 2,
        ROCKET = 100 * 10000 + typeIDInCrement * 3,
        OTHER_WEAPON = 100 * 10000 + typeIDInCrement * 99,

        // Material
        ORE = 200 * 10000 + typeIDInCrement * 0,

        ITEM_TYPE_END,
    }

    public string   iconPath        {get{return m_IconPath; }set{ m_IconPath = value;}}
    public string   name            {get{return m_Name; }set{ m_Name = value;}}
    public string   customName      {get{return m_CustomName; }set{ m_CustomName = value;}}
    public string   description     {get{return m_Description; }set{ m_Description = value;}}
    public int      value           {get{return m_Value; }set{ m_Value = value;}}
    public int      number          {get{return m_Number; }set{ m_Number = value;}}
    public int      typeID          {get{return m_TypeID; }set{ m_TypeID = value; }}
    public float    mass            { get { return m_Mass; } set { m_Mass = value; } }
    public float    volume          { get { return m_Volume; } set { m_Volume = value; } }

    public const int typeIDInCrement = 10000;

    protected string m_IconPath = "";
    protected string m_Name = "Item";
    protected string m_CustomName = "CustomItem";
    protected string m_Description = "Descriptioin";
    protected int     m_Number    = 0;//  数量
    protected int     m_Value     = 1;//  价值
    protected float   m_Mass      = 1;//  质量
    protected float   m_Volume    = 1;//  体积
    protected int     m_TypeID    = 0;//  识别Item的编号
    public    ItemType m_Type          = ItemType.NONE;//  识别Item类型

    public Item ()
    {
    }
    public static Item LoadItem(JsonData json)
    {

        IDictionary<string, JsonData> jsond = json.ValueAsObject();

        int id=0;
        if (jsond.ContainsKey("TypeID"))
            id = jsond["TypeID"].ValueAsInt();
        Item item = null;
        if (id>=0)
        {
            item = ItemManager.Instance.createItem(id);
        }
        else
        {
            string itemType = "NONE";
            if (jsond.ContainsKey("ItemType"))
                itemType = jsond["ItemType"].ValueAsString();
            item = ItemManager.Instance.createItem(itemType);
        }
        if(item != null)
            item.Load(json);
        return item;
    }
    public static Item CreateItemPrefab(JsonData json, ItemType itemType)
    {
        var item = ItemManager.Instance.createItem(itemType.ToString());
        item.LoadPrefab(json);
        return item;
    }
    static ItemType FindType(string typeStr)
    {
        for (int i = 0; i < (int)ItemType.ITEM_TYPE_END; ++i)
        {
            string checkItemType = ((ItemType)i).ToString();
            if (checkItemType == typeStr)
            {
                return (ItemType)i;
            }
        }
        return ItemType.NONE;
    }
    public virtual JsonData Save()
    {
        JsonData root = new JsonData();
        root["ItemType"] = (int)m_Type;
        root["TypeID"] = typeID;
        //root["IconPath"] = m_IconPath;
        //root["Name"] = m_Name;
        //root["CustomName"] = m_CustomName;
        //root["Description"] = m_Description;
        //root["Number"] = m_Number;
        return root;
    }

    public virtual JsonData SavePrefab(JsonData json)
    {
        JsonData root = json;
        if(root == null)
            root = new JsonData();
        root["ItemType"] = (int)m_Type;
        root["TypeID"] = typeID;
        root["IconPath"] = iconPath;
        root["Name"] = name;
        root["CustomName"] = customName;
        root["Description"] = description;
        root["Number"] = number;
        return root;
    }
    //  brief\ 从ItemList.data中，加载道具的预设值
    public virtual void LoadPrefab(JsonData json)
    {
        IDictionary<string, JsonData> jsond = json.ValueAsObject();
        if (jsond.ContainsKey("IconPath"))
            iconPath = jsond["IconPath"].ValueAsString();
        if (jsond.ContainsKey("Name"))
            name = jsond["Name"].ValueAsString();
        if (jsond.ContainsKey("Description"))
            description = jsond["Description"].ValueAsString();
        if (jsond.ContainsKey("Value"))
            value = jsond["Value"].ValueAsInt();
    }
    public virtual bool Load(JsonData json)
    {
        IDictionary<string, JsonData> jsond = json.ValueAsObject();
        if (jsond.ContainsKey("ItemType"))
            m_Type = (ItemType)jsond["ItemType"].ValueAsInt();
        if (jsond.ContainsKey("TypeID"))
            typeID = jsond["TypeID"].ValueAsInt();
        if (jsond.ContainsKey("CustomName"))
            customName = jsond["CustomName"].ValueAsString();
        if (jsond.ContainsKey("Number"))
            number = jsond["Number"].ValueAsInt();

        return true;
    }
    public virtual void Copy(Item item)
    {
        iconPath      = item.iconPath   ;
        name          = item.name       ;
        description   = item.description;
        number        = item.number     ;
        value         = item.value      ;
        m_Type          = item.m_Type       ;
        typeID        = item.typeID     ;
    }
}